package com.ningyue.water.entity.common;

import com.ningyue.water.entity.common.errors.ServiceErrors;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * Created by steven on 16/1/5.
 */
@Data
public class Result<D> extends BaseResult implements Serializable {

    private static final long serialVersionUID = -7647570604845078925L;

    @Getter
    @Setter
    private D data;

    // -------------------------- STATIC METHODS --------------------------

    public static <D> Result<D> wrapSuccessfulResult(D data) {
        Result<D> result = new Result<D>();
        result.data = data;
        result.success = true;
        return result;
    }

    public static <D> Result<D> wrapErrorResult(ServiceErrors error) {
        Result<D> result = new Result<D>();
        result.success = false;
        result.code = error.getCode();
        result.message = error.getMessage();
        return result;
    }


    /**
     * 错误Message中不要补充信息时的构造函数
     *
     * @return
     */
    public static <D> Result<D> wrapErrorResult(ServiceErrors error, Object... extendMsg) {
        Result<D> result = new Result<D>();
        result.success = false;
        result.code = error.getCode();
        result.message = String.format(error.getMessage(), extendMsg);
        return result;
    }

    public static <D> Result<D> wrapErrorResult(String code, String message) {
        Result<D> result = new Result<D>();
        result.success = false;
        result.code = code;
        result.message = message;
        return result;
    }
}